Skip to content

perf(core): a sealed drag restyles only the selection outline pieces that moved - #9092

Open
miguel-heygen wants to merge 3 commits into
mainfrom
perf/sealed-drag-chrome
Open

miguel-heygen wants to merge 3 commits into
mainfrom
perf/sealed-drag-chrome

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

A drag in a sealed preview no longer restyles the whole selection outline every frame. The frame draws a copy of Studio's selection outline with each drag step, and each step used to clear and rewrite the full inline style of every node of that copy: about 450 style writes per frame for a plain move. Now the copy's look is written once when it mounts, and each step writes only the place values that changed.

  • In a Chrome trace of a sealed move, the frame's inline style writes per frame drop from about 430 to about 3.
  • Gate: unchanged. This removes a sealed-only cost found while comparing the edit accuracy bench's smoothness in both modes; the row that gates that comparison comes in a separate PR.

Why

Studio may only seal its preview by default once it behaves the same in both modes, with no lag only the sealed preview has. The edit accuracy bench's merge queue run measures both modes: of 751 cases, 247 are smooth unsealed and 104 sealed. A Chrome trace of a failing sealed move showed the frame recalculating style and repainting the whole outline copy on every frame, which the unsealed preview never does: its outline lives in Studio's own page and only the moved values change.

Related work

Follow-up to #9068 (the merge queue's sealed edit accuracy run).

How

  • dragChrome.ts: build writes a node's look and pointer-events: none !important when the copy mounts. writeMovedPlace writes only the place values that differ from the last step. A place value the next step leaves out falls back to the node's look, or is removed when the look has none, which is what the old full rewrite left. The plane's transform is written only when it changes.
  • A remount (Studio sends nodes again) clears the remembered places, so its first step writes every place value.

Test plan

  • fails without the fix: "writes only the place that moved, and puts back the look a place no longer overrides" (packages/core/src/runtime/editing/dragChrome.test.ts) fails with the old drawDragChrome: a step that moves one node mutates every node's style.

  • dragChrome.test.ts and editingDraft.test.ts: 34 of 34.

  • packages/core/src/runtime/editing/, editingDraft.test.ts and editingPort.test.ts: 395 of 395. Studio frameDragChrome.test.ts: 7 of 7. tsc --noEmit in packages/core: exit 0.

  • Edit accuracy bench, the 30 move- cases of the PR grid, the preview in Studio's process as CI runs it, 3 runs interleaved, on a Linux machine with no GPU. Each build was made from one tree, so they differ only by this change:

    Build Sealed worst main-thread frame worse than unsealed Sealed smooth (unsealed: 30 of 30 each run)
    main 23, 20 and 25 of 30 cases (median +0.4 to +1.0 ms) 28, 29, 29 of 30
    this branch 16, 8 and 19 of 30 cases (median -1.2 to +0.5 ms) 30, 30, 29 of 30
  • With the frame in its own process (full Chrome), sealed moves still drop more frames than unsealed after this change: fewer than before, but not as few as unsealed. The rest is the cost of drawing a second process's frame, and is not changed here.

  • Unit tests added/updated

  • Manual testing performed

  • Documentation updated (if applicable)

  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

No visible change

The sealed preview is not on by default. In a sealed preview the outline looks and moves the same; it costs less to draw.

…that moved

On each drag step the sealed frame cleared and rewrote the whole inline style of
every node of Studio's selection outline copy, about 450 style writes a frame.
The look is now written once when the copy mounts, and each step writes only
the place values that changed.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Restore the base longhands when a shorthand override is removed. At packages/core/src/runtime/editing/dragChrome.ts:148, removing an inline inset also clears the mounted look’s left and top. Chrome moved the draft outline to (0, 0) on the next step, while the previous implementation restored the base position. Preserve the fast path for independent properties and rebuild the affected styles when declarations overlap.

Two smaller issues with the performance check. Chrome rounds the serialized plane transform, so comparing it with the raw generated string can still request a style write every step. The MutationObserver test also passes with the per-property write guard removed, because identical assignments can leave the attribute unchanged.

71 targeted tests passed. I verified the shorthand behavior in Chrome. The full edit-accuracy benchmark and compositor performance were unverified locally.

— tai (automated review)

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 273), smooth 59 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-root-z100
  • seqscaletwice-scale-px-r0-nested-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-root-z100
  • seqscaletwice-scale-px-r0-nested-z100

Quarantined, measured but not gated (0)

… shorthand goes

A place value that overlaps others, such as inset, rebuilds the node as before.
Only independent values (left, top, width and the like) are written alone, and
the plane's transform is compared with what was last written.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

All three points fixed at d39e3ea6.

  • Shorthands: a step writes single properties only when every place property is independent (left, top, width, height, transform, opacity, clip-path) and none was dropped. Otherwise the node is rebuilt as before this PR: look, then place, then pointer-events.
  • Plane transform: compared with the string last written, not with what the browser serialized.
  • Test: it now counts setProperty calls instead of observing attribute changes, so identical writes are seen.

fails without the fix (packages/core/src/runtime/editing/dragChrome.test.ts), both at 18863314:

  • "writes only the place that moved, counting every style write"
  • "rewrites a node's look and place when a place shorthand changes or goes"

Each also fails with one part removed: the value guard or the transform guard fails the first; the dropped-value rebuild or the shorthand rebuild fails the second. Core editing suites: 396 of 396, three runs. tsc --noEmit in packages/core: exit 0. Studio frameDragChrome.test.ts: 7 of 7. These run in happy-dom, which does not model shorthands, so they assert the writes; I have not rerun your Chrome check.

Corrections to the measurements in the description:

  • They were taken at 18863314, before this change, and have not been repeated.
  • The machine was shared and under varying load, with no CPU limit on the runs. Read them as indicative, not as a controlled benchmark.
  • The row named main is the same tree without this change, not the tip of main.
  • For the frame in its own process, the description says the rest of the gap is the cost of drawing a second process's frame. That is a hypothesis: contention on that machine was not ruled out.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at d39e3ea. Approve.

Read dragChrome.ts and dragChrome.test.ts whole at this head, and frameDragChrome.ts placeOf for what Studio sends as a place. Nothing was run here (no installed dependencies on this box); this is a read of the code.

The open change request, point by point:

  • A removed shorthand clearing the look's own values: resolved. writeMovedPlace now takes the incremental path only when every name in the last and next place is one of seven properties that do not overlap each other (left, top, width, height, transform, opacity, clip-path). Any dropped name, or a change while another name is present, goes through writeNode, which is the old full rewrite (look, place, then pointer-events). The new test covers both a changed and a dropped inset.
  • Plane transform compared against the serialized value: resolved. The comparison is against the string the code itself last wrote (planeAt), reset on clear.
  • Test passing without the per-property guard: resolved. The test now counts setProperty calls and asserts the third identical step adds none.

I also checked the incremental path against the old result: a first step after mount has an empty remembered place, so every value is written over the look in the same order as before; a place that carries pointer-events never takes the incremental path, so the forced none stays last.

Non-blocking:

  1. The body's fails without the fix: line names "writes only the place that moved, and puts back the look a place no longer overrides". That test is now two: "writes only the place that moved, counting every style write" and "rewrites a node's look and place when a place shorthand changes or goes". Worth a body edit.
  2. Studio builds a place from the element's inline style names, which are always longhands. A moved outline piece whose inline style carries any name outside the seven (for example right or bottom) takes the full rewrite for that piece each step. Correct, just not the fast path; the trace numbers in the body suggest the moved pieces do not hit this today.

Need: yes, measured cost only the sealed preview had · Size: 2 files, +69/-7 · Reuse: writeNode keeps the old rewrite as the fallback · Simpler: no · Structure: fine
Risk: low; the sealed preview is not on by default.

— Rames

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants